package com.tao.service.manager.impl;

import java.io.Serializable;
import java.util.List;

import com.tao.dao.manager.BaseDao;
import com.tao.domain.SubMenu;
import com.tao.util.Page;

public class BaseServiceImpl<T> {
	protected BaseDao<T> baseDao;
	
	public void setBaseDao(BaseDao<T> baseDao) {
		this.baseDao = baseDao;
	}

	public void add(T t) {
		baseDao.add(t);
	}

	public void delete(Serializable id) {
		baseDao.delete(id);	
	}

	public void update(T t) {
		baseDao.update(t);		
	}

	public T load(Serializable id) {
		return baseDao.load(id);
	}

	public List<T> list(String hql, Object[] args) {
		return baseDao.list(hql, args);
	}

	public void flush() {
		baseDao.flush();		
	}
	public List queryAll(String sql){
		return baseDao.queryAll(sql);
	}
	
	public List queryBySql(String sql) {
		return baseDao.queryBySql(sql);
	}
	   
	
	@SuppressWarnings("rawtypes")
	public Page queryForPage(int currentPage, int pageSize, String hql) {
				
		Page<T> page = new Page<T>();

		int allRow = baseDao.getAllRowCount(hql, null);

		int offset = page.countOffset(currentPage,pageSize); 

		List<T>list = baseDao.queryForPage(offset, pageSize, hql);
		
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        
		return page;
	}

	public int getAllRowCount(String hql, Object[] args) {
		return baseDao.getAllRowCount(hql, args);
	}
	
}

